package com.zcg.gasmanager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.JsonObject;
import com.zcg.gasmanager.dao.LogDao;
import com.zcg.gasmanager.dao.UserDao;
import com.zcg.gasmanager.model.Gas;
import com.zcg.gasmanager.model.Log;
import com.zcg.gasmanager.model.TYPE;
import com.zcg.gasmanager.model.User;
import com.zcg.gasmanager.service.UserService;
import com.zcg.gasmanager.utils.JsonUtil;
import com.zcg.gasmanager.utils.VeDate;

@Component("userService")
public class UserServiceImpl implements UserService {
	private UserDao userDao;
	private LogDao logDao;

	private Log log = new Log();
	private Gas gas = new Gas();
	
	@Override
	@Transactional
	public void add(User u) {
//		if(u.getNotes()!=null){
//			u.setNotes(u.getNotes()
//					.replaceAll("<p>", "")
//					.replaceAll("</p>", "")
//					.replaceAll("<br />",""));
//		}
		u.setFoundDate(new Date());
		gas.setNumber(0);
		gas.setBalance(100);
		u.setGas(gas);
		userDao.save(u);
		log.setMsg(VeDate.getStringDate()+":  save user");
		logDao.add(log);
	}

	@Override
	@Transactional
	public void delete(User u) {
		userDao.delete(u);
		log.setMsg(VeDate.getStringDate()+":  delete user");
		logDao.add(log);
	}

	@Override
	@Transactional
	public List<User> queryAll() {
		log.setMsg(VeDate.getStringDate()+":  queryAll user");
		logDao.add(log);
		return userDao.queryList();
	}

	public String filter(String message) {
		if (message == null)
			return (null);
		char content[] = new char[message.length()];
		message.getChars(0, message.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '"':
				result.append("&quot;");
				break;
			default:
				result.append(content[i]);
			}
		}
		return (result.toString());
	}

	@Override
	@Transactional
	public User queryById(User u) {
		User user = userDao.queryById(u);
		log.setMsg(VeDate.getStringDate()+":  queryById user");
		logDao.add(log);
		return user;
	}

	@Override
	@Transactional
	public void update(User u) {
		if(u.getNotes()!=null){
		u.setNotes(u.getNotes()
				.replaceAll("<p>", "")
				.replaceAll("</p>", "")
				.replaceAll("<br />",""));
		}
		log.setMsg(VeDate.getStringDate()+":  update user");
		logDao.add(log);
		userDao.update(u);
	}

	@Override
	@Transactional
	public boolean check(User u) {
		log.setMsg(VeDate.getStringDate()+":  check user");
		logDao.add(log);
		return userDao.load(u);
	}

	@Override
	@Transactional
	public boolean checkName(User u) {
		log.setMsg(VeDate.getStringDate()+": check user.name");
		logDao.add(log);
		return userDao.loadName(u);
	}

	@Override
	@Transactional
	public void updateType(TYPE userType) {
		log.setMsg(VeDate.getStringDate()+":  update user.userType");
		logDao.add(log);
		userDao.update(userType);
	}

	public UserDao getUserDao() {
		return userDao;
	}
	
	@Override
	@Transactional
	public String queryJsonLimit(int start, int limit) {
			List<User> users = userDao.queryLimit(start, limit);
			log.setMsg(VeDate.getStringDate()+": query user limit");
			logDao.add(log);
			JsonObject jsonObject = new JsonObject();
			jsonObject.add("rows", JsonUtil.toGsonTree(users));
			jsonObject.addProperty("total", userDao.getCount());
			String result = jsonObject.toString();
			return result;
	}

	@Override
	@Transactional
	public String queryJsonLimitByName(int page, int rows ,String name) {
		List<User> users = userDao.queryLimitByName(page, rows,name);
		log.setMsg(VeDate.getStringDate()+": query user limit");
		logDao.add(log);
		JsonObject jsonObject = new JsonObject();
		jsonObject.add("rows", JsonUtil.toGsonTree(users));
		jsonObject.addProperty("total", userDao.getCount());
		String result = jsonObject.toString();
		return result;
	}

	@Override
	@Transactional
	public String queryJsonLimitOrder(int page, int rows, String sort,
			String order) {
		List<User> users = userDao.queryLimitOrder(page, rows ,sort,order);
		log.setMsg(VeDate.getStringDate()+": query user limit and order");
		logDao.add(log);
		JsonObject jsonObject = new JsonObject();
		jsonObject.add("rows", JsonUtil.toGsonTree(users));
		jsonObject.addProperty("total", userDao.getCount());
		String result = jsonObject.toString();
		return result;
	}

	@Override
	@Transactional
	public String queryJsonLimitOrderByName(int page, int rows, String sort,
			String order, String name) {
		List<User> users = userDao.queryLimitOrderByName(page, rows,sort,order,name);
		log.setMsg(VeDate.getStringDate()+": query user limit and order by name");
		logDao.add(log);
		JsonObject jsonObject = new JsonObject();
		jsonObject.add("rows", JsonUtil.toGsonTree(users));
		jsonObject.addProperty("total", userDao.getCount());
		String result = jsonObject.toString();
		return result;
	}
	
	@Override
	@Transactional
	public void deleteIds(String ids) {
		userDao.deleteIds(ids);
		log.setMsg(VeDate.getStringDate()+":  delete ids");
		logDao.add(log);
	}
	
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public LogDao getLogDao() {
		return logDao;
	}
	
	@Resource
	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}
}
